/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

package com.huawei.trafficevents.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.dris.v1.DrisClient;
import com.huaweicloud.sdk.dris.v1.model.AddTrafficEventDTO;
import com.huaweicloud.sdk.dris.v1.model.BatchShowTrafficEventsRequest;
import com.huaweicloud.sdk.dris.v1.model.BatchShowTrafficEventsResponse;
import com.huaweicloud.sdk.dris.v1.model.CreateTrafficEventRequest;
import com.huaweicloud.sdk.dris.v1.model.CreateTrafficEventResponse;
import com.huaweicloud.sdk.dris.v1.model.DeleteTrafficEventRequest;
import com.huaweicloud.sdk.dris.v1.model.DeleteTrafficEventResponse;
import com.huaweicloud.sdk.dris.v1.model.EventLocation;
import com.huaweicloud.sdk.dris.v1.model.EventReferencePath;
import com.huaweicloud.sdk.dris.v1.model.ShowTrafficEventRequest;
import com.huaweicloud.sdk.dris.v1.model.ShowTrafficEventResponse;
import com.huaweicloud.sdk.dris.v1.model.UpdateTrafficEventDTO;
import com.huaweicloud.sdk.dris.v1.model.UpdateTrafficEventRequest;
import com.huaweicloud.sdk.dris.v1.model.UpdateTrafficEventResponse;
import com.huaweicloud.sdk.dris.v1.region.DrisRegion;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * RSU管理示例
 *
 * @author p00631112
 * @since 2023-01-05
 */
public class TrafficEventsSolutionDemo {
    private static final Logger logger = LoggerFactory.getLogger(TrafficEventsSolutionDemo.class);

    public static void main(String[] args) {
        String ak = "<YOUR AK>";
        String sk = "<YOUR SK>";

        // 创建DrisClient实例
        ICredential auth = new BasicCredentials()
            .withAk(ak)
            .withSk(sk);
        DrisClient client = DrisClient.newBuilder()
            .withCredential(auth)
            .withRegion(DrisRegion.valueOf("cn-north-4"))
            .build();

        try {
            String trafficEventId = createTrafficEvent(client);
            updateTrafficEvent(client, trafficEventId);
            listTrafficEvents(client);
            showTrafficEvent(client, trafficEventId);
            deleteTrafficEvent(client, trafficEventId);
        } catch (ConnectionException e) {
            logger.error("ConnectionException", e);
        } catch (RequestTimeoutException e) {
            logger.error("RequestTimeoutException", e);
        } catch (ServiceResponseException e) {
            logger.error("ServiceResponseException", e);
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }

    private static String createTrafficEvent(DrisClient client) {
        CreateTrafficEventRequest request = new CreateTrafficEventRequest();
        AddTrafficEventDTO body = new AddTrafficEventDTO();

        // 设置事件来源类型，取值范围：police：警方数据、government：政府数据、rsu：RSU上报数据等
        body.setEventSourceType("rsu");

        // 参考接口说明 https://support.huaweicloud.com/api-v2x/v2x_04_0101.html
        body.setEventClass("AbnormalTraffic");
        body.setEventType(100);

        // 参数说明：区域码，参考区域码查询。
        body.setAreaCode(518051);

        // 设置事件位置中心坐标
        EventLocation eventLocation = new EventLocation();
        eventLocation.setLat(BigDecimal.valueOf(35.01));
        eventLocation.setLon(BigDecimal.valueOf(113.02));
        body.setEventPosition(eventLocation);

        // 设置事件位置参考路径
        EventReferencePath referencePath = new EventReferencePath();
        referencePath.setPathRadius(100);
        List<EventLocation> path = new ArrayList<>();
        EventLocation pathPoint01 = new EventLocation();
        pathPoint01.setLat(BigDecimal.valueOf(35.0101));
        pathPoint01.setLon(BigDecimal.valueOf(113.002));
        path.add(pathPoint01);
        EventLocation pathPoint02 = new EventLocation();
        pathPoint02.setLat(BigDecimal.valueOf(35.0102));
        pathPoint02.setLon(BigDecimal.valueOf(113.002));
        path.add(pathPoint02);
        referencePath.setActivePath(path);

        List<EventReferencePath> referencePaths = new ArrayList<>();
        referencePaths.add(referencePath);
        body.setReferencePaths(referencePaths);

        // 设置事件所在位置说明
        body.setEventPositionName("北环大道1010");

        // 设置事件影响开始时间和结束时间
        body.setStartTime("2023-01-01T01:37:01Z");
        body.setEndTime("2023-01-02T01:37:01Z");

        request.withBody(body);
        CreateTrafficEventResponse response = client.createTrafficEvent(request);
        System.out.println(response.toString());
        return response.getEventId();
    }

    private static void updateTrafficEvent(DrisClient client, String trafficEventId) {
        UpdateTrafficEventRequest request = new UpdateTrafficEventRequest();
        request.setEventId(trafficEventId);
        UpdateTrafficEventDTO body = new UpdateTrafficEventDTO();
        body.setEventPositionName("北环大道1011");
        request.withBody(body);
        UpdateTrafficEventResponse response = client.updateTrafficEvent(request);
        System.out.println(response.toString());
    }

    private static void listTrafficEvents(DrisClient client) {
        BatchShowTrafficEventsRequest request = new BatchShowTrafficEventsRequest();
        BatchShowTrafficEventsResponse response = client.batchShowTrafficEvents(request);
        System.out.println(response.toString());
    }

    private static void showTrafficEvent(DrisClient client, String trafficEventId) {
        ShowTrafficEventRequest request = new ShowTrafficEventRequest();
        request.setEventId(trafficEventId);
        ShowTrafficEventResponse response = client.showTrafficEvent(request);
        System.out.println(response.toString());
    }

    private static void deleteTrafficEvent(DrisClient client, String trafficEventId) {
        DeleteTrafficEventRequest request = new DeleteTrafficEventRequest();
        request.setEventId(trafficEventId);
        DeleteTrafficEventResponse response = client.deleteTrafficEvent(request);
        System.out.println(response.toString());
    }
}
